package com.example.sls.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.example.sls.pojo.Result;
import com.example.sls.pojo.user.Users;
import com.example.sls.service.intserfaces.user.UsersService;
import com.example.sls.utils.LogUtil;
import com.example.sls.utils.Md5Util;
import com.example.sls.utils.VerifyCodeUtil;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.Map;

/**
 * 编者：蓝江富
 * 时间：2024/6/18 12:36
 * 登录、注册、登出
 */
@RestController
@RequestMapping("users")
public class Login {
  @Autowired
  UsersService usersService;

  @SaIgnore
  @PostMapping("register")
  public Result register(@Pattern(regexp = "^\\S{4,16}$") String username, @Pattern(regexp = "^\\S{4,16}$") String password) {

    //查询用户
    Users u = usersService.findByUsersName(username);
    if (u == null) {
      //没有占用,注册
      Users users=new Users();
      users.setUsername(username);
      users.setPassword(password);
      usersService.insert(users);
      return Result.success();
    } else {
      //占用
      return Result.error("用户名被占用");
    }
  }
  @SaIgnore
  @PostMapping("login")
  public Result login( String username, @Pattern(regexp = "^\\S{4,16}$") String password, String captchaCode) {
    //根据用户名查询用户
    LogUtil.log("用户名：",username,"\n密码\"",password,"\"加密后：", Md5Util.getMD5String(password),"\n验证码：",captchaCode);
    Jedis jedis=new Jedis();
    // redis中存储的验证码
    String verifyCode = jedis.get(captchaCode);
    jedis.close();
    LogUtil.log("redis验证码: ",verifyCode);
    // 代码块，验证码判断，开发阶段先注释掉不使用
    {
      if (verifyCode == null)
        return Result.error("验证码错误或已过期");
    }
    Users loginUsers = usersService.findByUsersName(username);
    //该用户是否存在
    if (loginUsers == null) {
      return Result.error("用户名不正确");
    }
    //判断密码是否正确,LoginUser中的password是密文
    if (Md5Util.getMD5String(password).equals(loginUsers.getPassword())){
      //登陆成功
//      Map<String,Object> claims =new HashMap<>();
//      claims.put("id",loginUsers.getId());
//      claims.put("username",loginUsers.getUsername());
      StpUtil.login(loginUsers.getId());
      return Result.success(StpUtil.getTokenInfo());
    }
    return  Result.error("密码错误");
  }

  @SaIgnore
  @DeleteMapping("logout")
  Result logout(){
    StpUtil.logout();
    return Result.success();
  }

  // 生成验证码
  @SaIgnore
  @GetMapping("getVerifyCode")
  public void getVerifyCode (HttpServletResponse response) {
    VerifyCodeUtil.function(response);
  }

  // 获取当前账号信息
  @GetMapping("me")
  Result me(){
    Users user=usersService.queryById(Integer.valueOf((String) StpUtil.getLoginId()));
    Map<String,Object> map=new HashMap<>();
    map.put("username",user.getUsername());
    map.put("avatar",user.getAvatar());
    map.put("roles",StpUtil.getRoleList());
    map.put("permission",StpUtil.getPermissionList());
    return Result.success(map);
  }
}
